package com.e3.template.tgf;

import java.util.ArrayList;
import java.util.List;

import com.e3.web.core.client.GridListener;
import com.e3.web.core.client.QueryListener;
import com.e3.web.core.model.SuperModel;
import com.e3.web.core.model.SuperQC;
import com.e3.web.widget.client.BaseAction;
import com.e3.web.widget.client.MyButton;
import com.e3.web.widget.client.MyRefDialog;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.user.client.Element;

public abstract class GFRefDialog<D extends SuperModel> extends MyRefDialog<D> implements QueryListener<D>,GridListener<D>{
	private SelectionListener<ButtonEvent> btnSelectListener = new SelectionListener<ButtonEvent>() {
		@Override
		public void componentSelected(ButtonEvent ce) {
			MyButton item = (MyButton)ce.getSource();
			onBtnMnuEvent(item.getAction());
		}
	};
	
	private D selectedModel;
	
	private ContentPanel contentPanel = new ContentPanel(new BorderLayout());
	
	private ToolBar toolbar = new ToolBar();
	
	private MyButton queryButton = new MyButton(TGFStateMachine.QUERY_ACTION);
	
	private TGFGridPanel<D> gridPanel;
	
	private TGFQueryDialog<D> queryDialog;
	
//	private FormPanel rapidQueryPanel;
	
	@Override
	protected void onRender(Element parent, int pos) {
		
		super.onRender(parent, pos);
		
		this.setSize(600, 450);
		this.setHeading(getRefDialogTitle());
		setLayout(new FitLayout());
		
//		BorderLayoutData bld = new BorderLayoutData(LayoutRegion.NORTH);
//		bld.setSplit(true);
//		bld.setSize((float)0.2);
//		contentPanel.add(getRapidQueryPanel(),bld);
		
		BorderLayoutData bld = new BorderLayoutData(LayoutRegion.CENTER);
		contentPanel.add(getGridPanel(),bld);
		
		initToolBar();
		
		contentPanel.setHeaderVisible(false);
		this.add(contentPanel);
	}
	
	@Override
	public void onRefWindowShow() {
		if(selectedModel!=null){
			List<D> models = new ArrayList<D>(1);
			models.add(selectedModel);
			getGridPanel().setGridValues(models);
		}
	}

	@Override
	public D getRefValue() {
		return selectedModel;
	}

	@Override
	public void setRefValue(D value) {
		selectedModel = (D)value;
	}
	
	private void onBtnMnuEvent(BaseAction action) {
		if(action == TGFStateMachine.QUERY_ACTION){
			onQuery();
		}
	}
	
	private void onQuery(){
		getQueryDialog().setVisible(true);
	}

	private void initToolBar(){
		queryButton.addSelectionListener(btnSelectListener);
		toolbar.add(queryButton);
		contentPanel.setTopComponent(toolbar);
	}
	
	public TGFGridPanel<D> getGridPanel(){
		if(gridPanel == null){
			gridPanel = createGridPanel();
			gridPanel.addGridListener(this);
		}
		return gridPanel;
	}

	
	protected TGFQueryDialog<D> getQueryDialog() {
		if(queryDialog == null){
			queryDialog = createQueryDialog();
			queryDialog.setLocalName(getCurrentLocalName());
			queryDialog.addQueryListener(this);
		}
		return queryDialog;
	}
	
//	protected FormPanel getRapidQueryPanel(){
//		if(rapidQueryPanel == null){
//			rapidQueryPanel = new FormPanel();
//			rapidQueryPanel.setHeaderVisible(false);
//			rapidQueryPanel.setAutoHeight(true);
//			List<Field<?>> fields = getRapidQueryFields();
//			if(fields != null){
//				LayoutContainer main = new LayoutContainer();
//				main.setLayout(new ColumnLayout());
//				int size = getColumnCount();
//				float colWidth = (float) (1.0/size);
//				FormData formData = new FormData("100%");
//				LayoutContainer lc = null;
//				for (int i = 0; i < fields.size(); i++) {
//					if(i%size==0){
//						lc = new LayoutContainer(new FormLayout());
//						main.add(lc,new ColumnData(colWidth));
//					}
//					lc.add(fields.get(i),formData);
//				}
//				rapidQueryPanel.add(main,new FormData("100%"));
//			}
//		}
//		return rapidQueryPanel;
//	}
	
	public void onQuerySuccessed(SuperQC qc,List<D> models) {
		getGridPanel().setGridValues(models);
	}

	public void onGridSelectionChanged(D model) {
		selectedModel = model;
	}

	public void onGridRowDoubClicked(D model) {
		onOk();
	}
	
	public List<D> getSelectedModels(){
		return getGridPanel().getSelectedModels();
	}
	
	public int getColumnCount(){
		return 2;
	}

	protected abstract String getRefDialogTitle();
	
	protected abstract TGFGridPanel<D> createGridPanel();
	
	protected abstract TGFQueryDialog<D> createQueryDialog();
	
	protected abstract String getCurrentLocalName();
	
//	protected abstract List<Field<?>> getRapidQueryFields();

}
